package com.huawei.ju;

/**
 * @description https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79?tpId=295&tqId=664&ru=%2Fpractice%2Fb49c3dc907814e9bbfa8437c251b028e&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj
 *  删除给出链表中的重复元素（链表中元素从小到大有序），使链表中的所有元素都只出现一次
 * @author wj
 * @date 2023-10-04
 */
public class BM15 {

  public static void main(String[] args) {
    BM15 bm15 = new BM15();
    ListNode listNode1 = new ListNode(1);
    ListNode listNode2 = new ListNode(1);
//    ListNode listNode3 = new ListNode(1);
    ListNode listNode3 = new ListNode(2);
    ListNode listNode4 = new ListNode(3);
    ListNode listNode5 = new ListNode(3);
    listNode4.next = listNode5;
    listNode3.next = listNode4;
    listNode2.next = listNode3;
    listNode1.next = listNode2;
    ListNode listNode = bm15.deleteDuplicates(listNode1);
    System.out.println(listNode);
  }

  /**
   * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
   *
   *
   * @param head ListNode类
   * @return ListNode类
   */
  public ListNode deleteDuplicates (ListNode head) {
    if (head == null || head.next == null) {
      return head;
    }
    ListNode temp = head;
    while (temp != null) {
      if (temp.next != null && temp.val == temp.next.val) {
        temp.next = temp.next.next;
      } else {
        temp = temp.next;
      }
    }
    return head;
  }

  public static class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }
}
